package com.feel.easycloud.controller;

import com.feel.easycloud.common.constant.CommonConstant;
import com.feel.easycloud.common.utils.AuthenticateUtils;
import com.feel.easycloud.entity.SysUser;
import com.feel.easycloud.enums.ApiResultEnum;
import com.feel.easycloud.handler.LoginHandler;
import com.feel.easycloud.handler.LoginHandlerFactory;
import com.feel.easycloud.service.EmailService;
import com.feel.easycloud.service.SystemUserService;
import com.feel.easycloud.utils.ApiResult;
import com.feel.easycloud.utils.CodeUtils;
import com.feel.easycloud.vo.CreateAndUpdateSystemUserVo;
import com.feel.easycloud.vo.login.ForgetPasswordRestEmailRequestVo;
import com.feel.easycloud.vo.login.LoginJwtVo;
import com.feel.easycloud.vo.login.LoginUserRequestVo;
import com.feel.easycloud.vo.login.LoinIdentityRequestVo;
import com.feel.easycloud.vo.singlebasevo.BasePageQueryVo;
import com.feel.easycloud.vo.singlebasevo.SingleEntityRequestVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SysUserController {

    @Autowired
    SystemUserService systemUserService;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    LoginHandlerFactory loginHandlerFactory;

    @Autowired
    EmailService emailService;

    @PostMapping("create/systemUser")
    public String createSystemUser(@RequestBody CreateAndUpdateSystemUserVo requestVo){
        systemUserService.createSystemUser(requestVo);
        return "success";
    }

    @PostMapping("update/systemUser")
    public String updateSystemUser(@RequestBody CreateAndUpdateSystemUserVo requestVo){
        systemUserService.updateSystemUser(requestVo);
        return "success";
    }

    @GetMapping("delete/systemUser")
    public String deleteSystemUser(SingleEntityRequestVo requestVo){
        systemUserService.deleteSystemUser(requestVo);
        return "success";
    }

    @GetMapping("get/systemUser")
    @ResponseBody
    public CreateAndUpdateSystemUserVo getSystemUser(SingleEntityRequestVo requestVo){
        return systemUserService.getSystemUser(requestVo);
    }

    @GetMapping("list/systemUser")
    @ResponseBody
    public PageInfo getSystemUserPageList(BasePageQueryVo requestVo){
        return systemUserService.getSystemUserPageList(requestVo);
    }

    @PostMapping("/api/auth/user/login")
    public ApiResult<LoginJwtVo> login(String loginType, @RequestBody LoginUserRequestVo requestVo){
        LoginHandler loginHandler = loginHandlerFactory.login(CommonConstant.LOGINMODEL.formCode(loginType));

        String code = redisTemplate.opsForValue().get(requestVo.getCodeKey());
        if(!requestVo.getCode().equalsIgnoreCase(code)){
            return ApiResult.fail(ApiResultEnum.CODE_100003);
        }

        LoinIdentityRequestVo loginVo = new LoinIdentityRequestVo();
        loginVo.setIdentity(requestVo.getPhone());
        loginVo.setSecretToken(requestVo.getSecret());
        LoginJwtVo loginJwtVo = loginHandler.login(loginVo);

        ApiResult<SysUser> loginResult = systemUserService.login(loginType, requestVo);

        if(!loginResult.isOk()){
            return ApiResult.fail(loginResult.getCode(),loginResult.getMsg());
        }

        Map<String, String> jwtMap = new HashMap<>();
        jwtMap.put("userId",loginResult.getData().getId());
        jwtMap.put("name",loginResult.getData().getName());
        jwtMap.put("phone",loginResult.getData().getPhone());
        String jwt = AuthenticateUtils.createJWT(jwtMap);
        LoginJwtVo res = new LoginJwtVo();
        res.setJwt(jwt);
        res.setUserId(loginResult.getData().getId());
        res.setUserName(loginResult.getData().getName());
        redisTemplate.opsForValue().set(loginResult.getData().getPhone(),jwt);
        loginJwtVo.setJwt(jwt);
        return ApiResult.success(loginJwtVo);
    }

    @PostMapping("/forgetPassword/sendEmail")
    public ApiResult sendEmail(@RequestBody ForgetPasswordRestEmailRequestVo requestVo){

        String codeKey = requestVo.getCodeKey();
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        String redisCode = valueOperations.get(codeKey);
        
        if(!requestVo.getCode().equalsIgnoreCase(redisCode)){
            return ApiResult.fail(ApiResultEnum.CODE_100003);
        }

        ApiResult<SysUser> sysUserApiResult = systemUserService.checkEmail(requestVo.getEmail());

        if(!sysUserApiResult.isOk()){
            return sysUserApiResult;
        }

        emailService.sendEamil(requestVo.getEmail(), CodeUtils.randomStr(6));
        return ApiResult.success("发送成功");
    }


}
